package me.xingyannian.zhaopin.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import me.xingyannian.zhaopin.dto.JobDto;
import me.xingyannian.zhaopin.entity.ChangCi;
import me.xingyannian.zhaopin.entity.Job;
import me.xingyannian.zhaopin.service.JobService;

import java.util.List;
import java.util.Map;
/**
 * @author xingyannian
 */
@Slf4j
public class JobDtoListener implements ReadListener<JobDto> {
    private final JobService jobService;
    private final ChangCi changCi;
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<Job> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    public JobDtoListener(JobService jobService, ChangCi changCi) {
        this.jobService = jobService;
        this.changCi = changCi;
    }
    /**
     * 这个每一条数据解析都会来调用
     *
     *
     */
    @Override
    public void invoke(JobDto jobDto, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", jobDto.toString());
        cachedDataList.add(getJob(jobDto, this.changCi));
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    /**
     * 在所有分析完成后执行的操作。
     *
     * @param analysisContext 分析上下文对象，包含所有分析的结果和信息。
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }
    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
        this.jobService.saveAll(cachedDataList);
        log.info("存储数据库成功！");
    }
    // 数据转换jobDto转job
    Job getJob(JobDto jobDto, ChangCi changCi){
        Job job = new Job();
        job.setChangCi(changCi);
        // 字符处理，去掉字符两端空格和换行符
        job.setSort(jobDto.getSort());
        job.setCompany(trimString(jobDto.getCompany()));
        job.setRegion(trimString(jobDto.getRegion()));
        job.setJobName(trimString(jobDto.getJobName()));
        job.setStage(trimString(jobDto.getStage()));
        job.setCode(trimString(jobDto.getCode()));
        job.setCount(jobDto.getCount());
        job.setTarget(trimString(jobDto.getTarget()));
        job.setDegree(trimString(jobDto.getDegree()));
        job.setDegreeLevel(trimString(jobDto.getDegreeLevel()));
        job.setOther(trimString(jobDto.getOther()));
        job.setLevel(trimString(jobDto.getLevel()));
        job.setAddress(trimString(jobDto.getAddress()));
        job.setContact(trimString(jobDto.getContact()));
        job.setSubject(trimString(jobDto.getSubject()));
        job.setRemark(trimString(jobDto.getRemark()));
        return job;
    }
    // 字符处理，去掉字符两端空格和换行符
    String trimString(String str) {
        return (str != null) ? str.trim() : null;
    }

}
